//
//  TTURLImageLoader.h
//  Tint
//
//  Created by Kevin Wu on 3/12/12.
//  Copyright (c) 2012 Telligenty. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "TTCore.h"
#import "TTAdditions.h"
#import "TTBasic.h"

#import "TTCaches.h"
#import "TTHTTPRequest.h"


@interface TTURLImageLoader : NSObject<TTOperationDelegate> {
    NSTimeInterval _defaultCacheTimeoutInterval;
    NSMutableArray *_downloadItems;
    TTDiskCache *_diskCache;
}

@property(nonatomic, assign) NSTimeInterval defaultCacheTimeoutInterval; // Default: 10*7 days


+ (TTURLImageLoader *)sharedObject;


- (void)loadImageWithAddress:(NSString *)address;

- (void)loadImageWithAddress:(NSString *)address 
        cacheTimeoutInterval:(NSTimeInterval)cacheTimeoutInterval // <=0 for default
                      target:(id)target 
                     success:(SEL)success 
                     failure:(SEL)failure 
            placeholderImage:(UIImage *)placeholderImage 
                failureImage:(UIImage *)failureImage;



- (void)cancelDownloadingAddress:(NSString *)address;
- (void)cancelRequestingTarget:(id)target;

- (BOOL)isAddressDownloading:(NSString *)address;
- (BOOL)isTargetRequesting:(id)target;

+ (NSString *)cacheKeyFromAddress:(NSString *)address;
- (void)removeCachedFileOfAddress:(NSString *)address;

@end




@interface TTURLImageLoaderDownloadItem : NSObject {
    NSString *_address;
    TTHTTPRequest *_request;
    NSMutableArray *_requestItems;
    NSTimeInterval _cacheTimeoutInterval;
}
@property(nonatomic, copy) NSString *address;
@property(nonatomic, retain) TTHTTPRequest *request;
@property(nonatomic, readonly) NSMutableArray *requestItems;
@property(nonatomic, assign) NSTimeInterval cacheTimeoutInterval;
@end

@interface TTURLImageLoaderRequestItem : NSObject {
    id _target;
    SEL _success;
    SEL _failure;
    UIImage *_placeholderImage;
    UIImage *_failureImage;
}
@property(nonatomic, assign) id target;
@property(nonatomic, assign) SEL success;
@property(nonatomic, assign) SEL failure;
@property(nonatomic, retain) UIImage *placeholderImage;
@property(nonatomic, retain) UIImage *failureImage;
@end
